package jp.co.hcs.attendancemanagement.dialog.logic;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jp.co.hcs.attendancemanagement.data.dao.RestTimeDAO;
import jp.co.hcs.attendancemanagement.data.entity.RestTime;
import jp.co.hcs.attendancemanagement.inputdailyattendance.actionform.InputDailyAttendanceForm;
import jp.co.hcs.attendancemanagement.inputdailyattendance.logic.dto.RestTimeDTO;
import jp.co.hcs.attendancemanagement.logic.LogicBase;
import jp.co.hcs.attendancemanagement.util.DateUtil;

/**
 * The Class RestTimeDetailLogic.
 */
public class RestTimeDetailLogic extends LogicBase {

  /** The rt dao. */
  private RestTimeDAO rtDao;

  /**
   * Load rest detail.
   * 
   * @param form the form
   * @throws SQLException the sQL exception
   */
  public void loadRestDetail(InputDailyAttendanceForm form, boolean isDateChanged, String year, String month, String day) throws SQLException {
    List<RestTimeDTO> result = form.getRestTimeInfoDTOList();

    RestTimeDTO restDto;
    if (isDateChanged || !form.isFindRestDetailFlag() || result == null || result.size() == 0) {
      if (result == null) {
        result = new ArrayList<RestTimeDTO>();
      } else {
        result.clear();
      }

      List<RestTime> restTimeList =
          getRtDao().retrieveRestTimeList(form.getUserId(), day, year + month);
      if (restTimeList != null) {
        RestTime rtObj;
        for (int i = 0; i < restTimeList.size(); i++) {
          rtObj = restTimeList.get(i);
          restDto = new RestTimeDTO();
          restDto.setTakingRestNum(i + 1);
          restDto.setComment(rtObj.getComment());
          restDto.setRestEndTime(DateUtil.convertDateToHourFormat(rtObj.getRestEndTime()));
          restDto.setRestStartTime(DateUtil.convertDateToHourFormat(rtObj.getRestStartTime()));
          restDto.setRestTime(DateUtil.convertDateToHourFormat(rtObj.getRestTime()));
          result.add(restDto);
        }
      }

      form.setFindRestDetailFlag(true);
      form.setDateChangeFlag(false);
    }

    for (int i = result.size(); i < 5; i++) {
      restDto = new RestTimeDTO();
      restDto.setTakingRestNum(i + 1);
      result.add(restDto);
    }
    form.setRestTimeInfoDTOList(result);
  }

  /**
   * Gets the rt dao.
   * 
   * @return the rt dao
   */
  public RestTimeDAO getRtDao() {
    if (rtDao == null) {
      rtDao = daoFactory.getRestTimeDAO();
    }

    return rtDao;
  }

  /**
   * Sets the rt dao.
   * 
   * @param rtDao the new rt dao
   */
  public void setRtDao(RestTimeDAO rtDao) {
    this.rtDao = rtDao;
  }

}
